package com.guigu.erp.utils;

import com.guigu.erp.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

@Component
public class NumberUtils {
    @Autowired
    private DesignFileService designFileService;

    @Autowired
    private DesignModuleService designModuleService;

    @Autowired
    private ManufactureDesignProcedureService manufactureDesignProcedureService;

    @Autowired
    private StorageCellService storageCellService;

    @Autowired
    private StorageGatherService storageGatherService;

    @Autowired
    private ManufactureApplyService manufactureApplyService;

    @Autowired
    private ManufactureManufactureService manufactureManufactureService;

    @Autowired
    private StoragePayService storagePayService;

    /**
     * 获取生产编号
     * @param firstKind I级分类编号
     * @param secondKind II级分类编号
     * @param thirdKind III级分类编号
     * @return
     */
    public String getProductNumber(String firstKind,String secondKind,String thirdKind){
        //拼接类型编号100
        StringBuffer buffer=new StringBuffer("100");
        // 拼接各级分类编号
        buffer.append(firstKind)
                .append(secondKind)
                .append(thirdKind);
        // 获取当前最大的流水号
        String flowNumber = designFileService.getMaxFlowNumber();
        int temp = Integer.parseInt(flowNumber);
        temp ++;
        // 补齐6位字符串
        String newFlowNumber = String.format("%06d", temp);
        String number = buffer.append(newFlowNumber).toString();
        return number;
    }


    /**
     * 生成各种设计单编号：
     * 物料设计单编号	200+当前日期+4位流水号
     * 工序设计单编号	201+当前日期+4位流水号
     * 生产计划编号	300+当前日期+4位流水号
     * 派工单编号	301+当前日期+4位流水号
     * 库存编号	400+当前日期+4位流水号
     * 入库单编号	401+当前日期+4位流水号
     * 出库单编号	402+当前日期+4位流水号
     * @param type 设计单类型编号
     * @return
     */
    public String getTypeNumber(String type) {
        // 拼接类型编号100
        StringBuffer buffer = new StringBuffer(type);

        // 当前日期 格式：YYYYMMDD
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        String date = sdf.format(now);
        buffer.append(date);

        // 根据类型获取对应设计单的最大的设计单编号
        String designId = "";
        switch (type) {
            case "200":
                designId = designModuleService.getMaxDesignId();
                break;
            case "201":
                designId = manufactureDesignProcedureService.getMaxDesignId();
                break;
            case "300":
                designId = manufactureApplyService.getMaxDesignId();
                break;
            case "301":
                designId = manufactureManufactureService.getMaxFlowNumber();
                break;
            case "400":
                designId = storageCellService.getMaxDesignId();
                break;
            case "401":
                designId = storageGatherService.getMaxDesignId();
                break;
            case "402":
                designId = storagePayService.getMaxDesignId();
                break;
        }

        if(designId == null)
            return buffer.append("0001").toString();

        // 获取当前设计单编号的4位流水号  xxxYYYYMMDDxxxx
        String flowNumber = designId.substring(11);
        // 获取当前设计单编号的年份
        String year = designId.substring(3, 7);
        String number = "";
        if(year.equals(date.substring(0, 4))){
            int temp = Integer.parseInt(flowNumber);
            temp ++;
            // 补齐4位字符串
            String newFlowNumber = String.format("%04d", temp);
            number = buffer.append(newFlowNumber).toString();
        } else {
            number = buffer.append("0001").toString();
        }
        return number;
    }
}
